define(function () {
    return function (pane) {
        /**
         * 导航
         */
        {
            pane.html(utils.getHtml('default_table'));

            utils.addFilterGroupbtn(pane, {
                name: 'type',
                text: '全部类型',
                filters: dicts.finance_type
            });

            utils.addFilterDropdown(pane, {
                name: 'operation',
                text: '全部操作',
                dropdown: dicts.finance_operation
            });
        }

        /**
         * 统计
         */
        {
            var analyse = function () {
                adminApi('finances_analyse').then(function (rows) {
                    $('.content-header .filter-type[data-name="type"] .badge').text('');
                    var total = 0;
                    for (var i in rows) {
                        var row = rows[i];
                        $('.type-' + row.type, pane).text((row.count || 0) + '个, ' + (row.amount ? row.amount.toFixed(2) : 0));
                        total += (row.count || 0);
                    }
                    $('.type-all', pane).text(total);
                });
            }
            analyse();
        }

        //初始化工具
        utils.addTools(pane, [
            {className: 'refresh-btn', icon: 'fa-refresh', text: '刷新'}
        ]);
        $(pane).on('click', '.refresh-btn', function () {
            if (table) {
                table.api().draw();
            }
            analyse();
        });

        //初始化表格
        var table = utils.dataTable(pane, {
            columns_arrays: [
                ["id", "#"],
                ["partner_id", "合伙人", false, function (i, t, row) {
                    var user = row.partner_user;
                    if (!user) {
                        return '';
                    }
                    return (user.image ? '<img src="' + user.image + '?imageView2/1/w/150/h/150/q/75"> ' : '') + '<div>' + (user.nickname || user.tel) + '</div><div>「ID:' + (user.id) + '」</div>';
                }, 'filterable'],
                ["user_id", "账户", false, function (s, t, row) {
                    var user = row.user;
                    if (!user) {
                        return '';
                    }
                    return (user.image ? '<img src="' + user.image + '?imageView2/1/w/150/h/150/q/75"> ' : '') + '<div>' + (user.nickname || user.tel) + '</div><div>「ID:' + (user.id) + '」</div>';
                }, 'filterable'],
                ["order_id", "订单号", false, function (s, t, row) {
                    return '<div class="show-order" style="cursor: pointer; color: #00a7d0;">' + (row.order_id || '') + '</div>';
                }],
                ["note", "备注", false],
                ["amount", "金额/费用", false, utils.priceRender()],
                ["type", "入/出", false, utils.dictRender('finance_type')],
                ["operation", "操作", false, utils.dictRender('finance_operation')],
                ["by", "方式", false],
                ["status", "状态", false, utils.dictRender('finance_status')],
                ["created_at", "创建时间", false, utils.dateTimeRender()]
            ],
            sorting: [[0, 'desc']],
            fnServerData: function (paramData) {
                return adminApi('finances_browse', paramData);
            }
        });


        table.on('click', '.show-order', utils.tableEvent(table, function (idx, rowData) {
            //订单列表
            var html = $(utils.getHtml('form_assign', {title: '订单列表 - ' + rowData.order_id}));
            html.addClass('modal-fullscreen');
            html.modal('show').on('hidden.bs.modal', function (e) {
                html.remove();
            });
            var ordersTable = utils.dataTable(html, {
                columns_arrays: [
                    ["id", "#"],
                    ["mediumId", "代理", false, function (i, t, row) {
                        var user = row.agency;
                        if (!user) {
                            return '';
                        }
                        return (user.image ? '<img src="' + user.image + '?imageView2/1/w/150/h/150/q/75"> ' : '') + '<div>' + (user.nickname || user.tel) + '</div><div>「ID:' + (user.id) + '」</div>';
                    }, 'filterable'],
                    ["user_id", "用户", false, function (i, t, row) {
                        var user = row.user;
                        if (!user) {
                            return '';
                        }
                        return (user.image ? '<img src="' + user.image + '?imageView2/1/w/150/h/150/q/75"> ' : '') + '<div>' + (user.nickname || user.tel) + '</div><div>「ID:' + (user.id) + '」</div>' + '<div>订单数: ' + user.order_count + '/' + user.finish_count + '/' + user.sham_count + '&nbsp;&nbsp;金额: ' + user.amount + '/' + user.frozen + '</div>';
                    }, 'filterable'],
                    ["pictUrl", "图片", false, utils.imageRender('')],
                    ["auctionId", "商品名", false, function (i, t, row) {
                        return '<div style="max-width: 160px;">' + (row.auctionTitle || '') + '</div>';
                    }, 'filterable'],
                    ["exNickName", "店铺", false, function (i, t, row) {
                        return '<div style="max-width: 160px;">' + (row.exShopTitle || '') + '</div>';
                    }, 'filterable'],
                    ["payStatus", "状态", false],
                    ["id", "返现状态", false, function (i, t, row) {
                        return '<div>付款金额:' + row.totalAlipayFeeString + '</div>' +
                            '<div>预估收入:￥' + row.feeString + '/' + row.discountAndSubsidyToString + '%</div>' +
                            '<div>返现状态:' + row.amount_status + '</div>' +
                            '<div>返现金额:<span class="badge" style="background-color: #00a65a!important; cursor: pointer;">￥' + (row.pay_amount || 0) + '</span></div>' +
                            '<div>邀请返现:<span class="badge" style="background-color: #f39c12!important; cursor: pointer;">￥' + (row.inviter_pay_amount || 0) + '</span></div>' +
                            '<div>代理收入:<span class="badge" style="background-color: #3c8dbc!important; cursor: pointer;">￥' + (row.agency_amount || 0 ) + '</span></div>' +
                            '<div>上级收入:<span class="badge" style="background-color: #3c8dbc!important; cursor: pointer;">￥' + (row.inviter_agency_amount || 0 ) + '</span></div>' +
                            '<div>预计收入:<span class="badge" style="background-color: #dd4b39!important; cursor: pointer;">￥' + (row.income || 0) + '</span></div>';
                    }],
                    ["mediumId", "媒体", false, function (i, t, row) {
                        return row.mediumName;
                    }, 'filterable'],
                    ["adPositionId", "广告位", false, function (i, t, row) {
                        return row.adPositionName;
                    }, 'filterable'],
                    ["agency_settle_id", "结算号", false, false, 'filterable'],
                    ["createTime", "下单日期", true, utils.dateTimeRender()],
                    ["created_at", "创建日期", false, utils.dateTimeRender()]
                ],
                sorting: [[0, 'desc']],
                fnServerData: function (paramData) {
                    if (rowData.operation == 'ReturnAmount' || rowData.operation == 'InviterAmount' || rowData.operation == 'ReturnBack' || rowData.operation == 'InviterBack' || rowData.operation == 'ReturnPay') {
                        paramData.push({name: 'id', value: rowData.order_id});
                    } else if (rowData.operation == 'AgencySettle' || rowData.operation == 'InviterAgencySettle') {
                        paramData.push({name: 'agency_settle_id', value: rowData.order_id});
                    }
                    return adminApi('orders_browse', paramData);
                }
            });
        }));
    }
});